/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.bi.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.bi.entity.AliveMember;
import com.boxin.ims.modules.bi.dao.AliveMemberDao;

/**
 * 每天活跃用户Service
 * @author XiaoQian
 * @version 2014-06-19
 */
@Component
@Transactional(readOnly = true)
public class AliveMemberService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(AliveMemberService.class);
	
	@Autowired
	private AliveMemberDao aliveMemberDao;
	
	public AliveMember get(Long id) {
		return aliveMemberDao.findOne(id);
	}
	
	public Page<AliveMember> find(Page<AliveMember> page, AliveMember aliveMember) {
		DetachedCriteria dc = aliveMemberDao.createDetachedCriteria();
		if (aliveMember.getUser()!=null && aliveMember.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", aliveMember.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(aliveMember.getName())){
			dc.add(Restrictions.like("name", "%"+aliveMember.getName()+"%"));
		}
		if(StringUtils.isNotEmpty(aliveMember.getDay()) ){
			dc.add(Restrictions.eq("day", aliveMember.getDay()));
		}
		
		dc.addOrder(Order.desc("exchangeCount"));
		
		return aliveMemberDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(AliveMember aliveMember) {
		aliveMemberDao.save(aliveMember);
	}
	
	
	public Long getAliveMemberCountByDay(Long userId ,String  day){
		DetachedCriteria dc = aliveMemberDao.createDetachedCriteria();
			dc.add(Restrictions.eq("user.id", userId));
			dc.add(Restrictions.eq("day", day));
		return aliveMemberDao.count(dc);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		aliveMemberDao.deleteById(id);
	}
	
}
